#include "lvImg.h"
#include "custom_mem.h"
struct lvImg_res_gc_s *lvImg_gc_obj_add(struct lvImg_res_gc_s *gc_img,lv_img_dsc_t *img)
{
    struct lvImg_res_gc_s *head = (struct lvImg_res_gc_s *)custom_malloc(sizeof(struct lvImg_res_gc_s));
    head->img = img;
    
    if(gc_img)
    {
        head->next = gc_img;
    }
    else
    {
        head->next = NULL;
    }
    return head;
}

void fs_lv_img_free(lv_img_dsc_t * img)
{
    #ifdef PSRAM_HEAP
        custom_free_psram(img);
    #else
        custom_free(img);
    #endif
}

void lvImg_gc_loop(struct lvImg_res_gc_s *gc_img)
{
   struct lvImg_res_gc_s *head,*tmp_head;
   head = gc_img;
   while(head)
   {
        fs_lv_img_free(head->img);
        tmp_head = head;
        head = head->next;
        custom_free(tmp_head);
   } 
}

void *get_fs_lv_img(const char *filename)
{
    lv_img_dsc_t * img = NULL;
    void *fp = osal_fopen(filename,"rb");
    if(fp)
    {
        uint32_t filesize = osal_fsize(fp);
        #ifdef PSRAM_HEAP
            img = custom_malloc_psram(filesize);
        #else
            img = custom_malloc(filesize);
        #endif
        osal_fread(img,1,filesize,fp);
        osal_fclose(fp);
        img->data = (const uint8_t *)((uint32_t)&img->data + 4);
    }
    return img;
}

